package com.link.base.base.mvg.service;

import com.link.base.base.mvg.dao.mybatis.basic.mapper.MvgMapper;
import com.link.base.base.mvg.model.Mvg;
import com.link.core.basic.controller.BasicControllerException;
import com.link.core.basic.dao.KeyGenerateDao;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.Filter;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.BeanTools;
import com.link.core.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author unknown
 * @date unknown
 * @comments mvg服务
 */
@Service
@Scope("prototype")
public class MvgServiceImpl implements MvgService {
    /**启用的mapper名称*/
    private String mvgMapperName;
    @Resource
    private KeyGenerateDao keyGenerateDao;

    @Resource
    private AfterMvgOperaService afterMvgOperaService;

    @Override
    public List<HashMap<String, String>> queryLeftListPage(BasicModel entity) throws Exception {
        return getMvgMapper().queryLeftListPage(entity);
    }

    @Override
    public List<HashMap<String, String>> queryRightListPage(BasicModel entity) throws Exception {
        return getMvgMapper().queryRightListPage(entity);
    }

    @Override
    public void insert(Mvg entity) throws Exception {
        if (null == entity.getMvgParentId()) {
            throw new ServiceException("MVG-001");
        }
        if (null == entity.getMvgSubsetId()) {
            throw new ServiceException("MVG-002");
        }
        entity.setId(keyGenerateDao.keyGenerate());
        getMvgMapper().insert(entity);

        /**
         * @author huanglongjian
         * @ 后续操作
         */
        afterMvgOperaService.afterOperation(entity, this.mvgMapperName);
    }

    @Override
    public Long queryDefault(Mvg entity) throws Exception {
        Long defMvgStId = null;
        QueryParams qps = new QueryParams();
        BeanUtils.copyProperties(entity, qps);
        qps.addFilter(new Filter(entity.getDefField(), "Y"));
        BasicModel t = (BasicModel) BasicModel.transformClass(entity, qps);
        BeanUtils.copyProperties(entity, t);
//      t.setConditionSql(conditionSql);
        t.setPageFlag(false);
        t.setTotalFlag(false);
        List list = queryRightListPage(t);
        if (list.size() == 1) {
            defMvgStId = ((BasicModel) list.get(0)).getId();
        }
        return defMvgStId;
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public Long batchInsert(List<Mvg> list) throws Exception {
        Long defMvgStId = null;
        if (list.get(0).getQueryDefaultFlag()) {
            defMvgStId = queryDefault(list.get(0));
        }
        for (int i = 0; list != null && i < list.size(); i++) {
            insert(list.get(i));
        }

        /**
         * @author huanglongjian
         * @ 后续操作
         */
        afterMvgOperaService.afterOperation(list.get(0), this.mvgMapperName);

        return defMvgStId;
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public Long batchDeleteById(List<Mvg> list) throws Exception {
        Long defMvgStId = null;
        if (list.get(0).getQueryDefaultFlag()) {
            defMvgStId = queryDefault(list.get(0));
        }
        for (int i = 0; list != null && i < list.size(); i++) {
            deleteById(list.get(i));
        }

        /**
         * @author huanglongjian
         * @ 后续操作
         */
        afterMvgOperaService.afterOperation(list.get(0), this.mvgMapperName);

        return defMvgStId;
    }

    @Override
    public void deleteById(Mvg entity) throws Exception {
        if (null == entity.getMvgParentId()) {
            throw new ServiceException("MVG-001");
        }
        if (null == entity.getMvgSubsetId()) {
            throw new ServiceException("MVG-002");
        }
        getMvgMapper().deleteById(entity);

        /**
         * @author huanglongjian
         * @ 后续操作
         */
        afterMvgOperaService.afterOperation(entity, this.mvgMapperName);
    }

    @Override
    public void deleteAllById(Mvg entity) throws Exception {
        if (null == entity.getMvgParentId()) {
            throw new ServiceException("MVG-001");
        }
        getMvgMapper().deleteAllById(entity);

        Mvg mvgItem = new Mvg();
        mvgItem.setMvgParentId(entity.getMvgParentId());
        //清空主表默认值--yrf
        setDefaultIdUpdate(mvgItem);
        /**
         * @author huanglongjian
         * @ 后续操作
         */
        afterMvgOperaService.afterOperation(entity, this.mvgMapperName);
    }

    /**
     * 根据mapper名称，获取mapper实现类
     *
     * @return 得到的mapper实现类
     */
    private MvgMapper getMvgMapper() throws Exception {
        String mapper = "Mapper";
        if (StringUtils.isBlank(this.mvgMapperName)) {
            throw new BasicControllerException("mvgMapperName名称不能为空，请检查程序！");
        }
        if (!this.mvgMapperName.contains(mapper)) {
            this.mvgMapperName = this.mvgMapperName + "MvgMapper";
        }
        //根据名称获取目标mapper
        MvgMapper mvgMapper = (MvgMapper) BeanTools.getBean(this.mvgMapperName);
        return mvgMapper;
    }


    @Override
    public String getMvgMapperName() {
        return mvgMapperName;
    }

    @Override
    public void setMvgMapperName(String mvgMapperName) {
        this.mvgMapperName = mvgMapperName;
    }

    @Override
    public void setDefaultIdUpdate(Mvg entity) throws Exception {
        if (null == entity.getMvgParentId()) {
            throw new ServiceException("MVG-001");
        }
        getMvgMapper().setDefaultIdUpdate(entity);
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void mvgModify(List<Mvg> list) throws Exception {
        for (int i = 0; i < list.size(); i++) {
            Mvg mvgItem = list.get(i);
            if ("NEW".equals(mvgItem.getRow_status())) {
                //往中间表插入数据
                insert(mvgItem);
            } else if ("DELETE".equals(mvgItem.getRow_status())) {
                //删除中间表数据
                deleteById(mvgItem);
            } else if ("DEFAULT".equals(mvgItem.getRow_status())) {
                //设置默认主表数据
                setDefaultIdUpdate(mvgItem);
            } else {
                throw new ServiceException("MVG-003");
            }
        }
    }

}
